package com.until.excption;

import com.until.response.AbsResponseBody;
import com.until.response.RestfulCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author AdminUser
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public RestfulCode.CodeSatCode bizExceptionHandler(HttpServletRequest req, BizException e){
        logger.error("发生业务异常！原因是：{}",e.errorMsg());
        return RestfulCode.getFreeResult(e.errorCode,e.errorMsg());
    }

    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public RestfulCode.CodeSatCode exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("发生空指针异常！原因是:",e);
        return RestfulCode.getFreeResult(AbsResponseBody.NULL_POINTER_EXCEPTION);
    }

    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public RestfulCode.CodeSatCode exceptionHandler(HttpServletRequest req, Exception e){
        logger.error("未知异常！原因是:",e);
        return RestfulCode.getFreeResult(AbsResponseBody.INTERNAL_SERVER_ERROR);
    }
}
